package ca.nfb.media.nfbPlayer.views
{
	import ca.nfb.media.nfbPlayer.ui.NFBImage;
	import ca.turbulent.media.events.PyroEvent;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.ByteArray;
	
	public class Watermark extends NFBPlayerView
	{
		public static const WATERMARK_ALIGN_BOTTOM_RIGHT		:String = "watermarkAlignBottomRight";
		public static const WATERMARK_ALIGN_BOTTOM_LEFT			:String = "watermarkAlignBottomLeft";
		public static const WATERMARK_ALIGN_BOTTOM_CENTER		:String = "watermarkAlignBottomCenter";
		public static const WATERMARK_ALIGN_CENTER				:String = "watermarkAlignCenter";
		public static const WATERMARK_ALIGN_TOP_RIGHT			:String = "watermarkAlignTopRight";
		public static const WATERMARK_ALIGN_TOP_LEFT			:String = "watermarkAlignTopLeft";
		public static const WATERMARK_ALIGN_TOP_CENTER			:String = "watermarkAlignTopCenter";
		
		private var holder			:MovieClip = new MovieClip();
		private var isLoaded		:Boolean = false;
		private var loader			:Loader;
		private var _hPadding		:Number = 0;
		private var _vPadding		:Number = 0;
		private var _watermarkURL	:String = "";
		
		private var _alignMode		:String = Watermark.WATERMARK_ALIGN_BOTTOM_RIGHT;
		 
		public function Watermark()
		{
			super();
			this.visible = false;
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);
			
			holder.addChild(loader);	
			addChild(holder);
			startLoading();
		}
		
		public function startLoading():void
		{
			_hPadding = core.watermarkHPadding;
			_vPadding = core.watermarkVPadding;
			if (core.watermarkURL != "" && core.showWatermark)
			{
				if (!this.isLoaded)
				{
					this.dispatchEvent(new Event(NFBImage.IMAGE_LOADING));
					_watermarkURL = core.previewImageURL;
				}
				else
				{
					if (this._watermarkURL != core.watermarkURL)
					{
						this.dispatchEvent(new Event(NFBImage.IMAGE_LOADING));
						this.isLoaded = false;
						_watermarkURL = core.previewImageURL;
					}
				}
				loader.load(new URLRequest(_watermarkURL));
			}	
			
		}
		
		private function onComplete(evt:Event):void
		{
			var totalWidth:Number = holder.width;
			var totalHeight:Number = holder.height;
				
			this.dispatchEvent(new Event(NFBImage.IMAGE_LOADED));	
			align();
			if (core.currentState == NFBPlayer.DEFAULT_STATE)
			{
				this.visible = true;
			}
		}
		
		private function onError(evt:IOErrorEvent):void
		{
			
		}
		
		override protected function newStreamInit(evt:PyroEvent):void 
		{
			startLoading();
		}
		
		private function align():void
		{
			switch(core.watermarkAlignMode)
			{
	
				case Watermark.WATERMARK_ALIGN_BOTTOM_RIGHT:
				holder.x = player.videoWidth - (holder.width + this._hPadding);
				holder.y = player.videoHeight - (holder.height + this._vPadding);
				break;
				
				case Watermark.WATERMARK_ALIGN_BOTTOM_CENTER:
				holder.x = player.videoWidth/2 - (holder.width/2);
				holder.y = player.videoHeight - (holder.height + this._vPadding);
				break;
				
				case Watermark.WATERMARK_ALIGN_BOTTOM_LEFT:
				holder.x = this._hPadding;
				holder.y = player.videoHeight - (holder.height + this._vPadding);
				break;
				
				case Watermark.WATERMARK_ALIGN_TOP_RIGHT:
				holder.x = player.videoWidth - (holder.width + this._hPadding);
				holder.y = this._vPadding;
				break;
				
				case Watermark.WATERMARK_ALIGN_TOP_CENTER:
				holder.x = player.videoWidth/2 - (holder.width/2);
				holder.y = this._vPadding
				break;
				
				case Watermark.WATERMARK_ALIGN_TOP_LEFT:
				holder.x = this._hPadding;
				holder.y = this._vPadding;
				break;
				
				case Watermark.WATERMARK_ALIGN_CENTER:
				holder.x = player.videoWidth/2 - (holder.width/2);
				holder.y = player.videoHeight/2 - (holder.height/2);
				break;
			}	
			
		}
		
		override protected function fullscreenHandler(evt:FullScreenEvent):void
		{
			if (core.showWatermark && core.watermarkURL != "" && this.isLoaded)
			{
				align();	
			}
			
		}
		
		override protected function stageResized(evt:Event=null):void
		{
			if (core.showWatermark && core.watermarkURL != "" && this.isLoaded)
			{
				align();	
			}
		}
		
		override protected function coreStateUpdated(evt:Event=null):void
		{
			if (core.currentState == NFBPlayer.DEFAULT_STATE)
			{
				this.visible = true;
			}
			else
			{
				this.visible = false;
			}
		}
		
	}
}